// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
let base = require("panelBase");
let viewConfig = {
    btn_open:{
        node:null,
        name:"coinNode/btn_open",
        eventType:"click",
        cb:"onBtnOpenClick",
    },
    coinLabel:{
        node:null,
        name:"coinNode/coinLabel",
    },
    btn_tx:{
        node:null,
        name:"txCoinNode/btn_tx",
        eventType:"click",
        cb:"onBtnTxClick",
    },
    txCoin:{
        node:null,
        name:"txCoinNode/txCoinLabel",
    },
    btn_rank:{
        node:null,
        name:"btn_rank",
        eventType:"click",
        cb:"onBtnRankClick",
    },
    btn_rule:{
        node:null,
        name:"btn_rule",
        eventType:"click",
        cb:"onBtnRuleClick",
    },
    btn_myInfo:{
        node:null,
        name:"btn_myInfo",
        eventType:"click",
        cb:"onBtnMyinfoClick",
    },
    btn_audio:{
        node:null,
        name:"btn_audio",
        eventType:"click",
        cb:"onBtnAudioClick"
    },
    btn_sign:{
        node:null,
        name:"btn_sign",
        eventType:"click",
        cb:"onBtnSignClick"
    },
    btn_task:{
        node:null,
        name:"btn_task",
        eventType:"click",
        cb:"onBtnTaskClick"
    },
    btn_turnTable:{
        node:null,
        name:"btn_turntable",
        eventType:"click",
        cb:"onBtnTurntableClick",
    },

    btn_ggk:{
        node:null,
        name:"btn_ggk",
        eventType:"click",
        cb:"onBtnGGKClick"
    },
    hbNode:{
        node:null,
        name:"hbNode",
        eventType:"click",
        cb:"onBtnhbClick"
    },
    totalCoin:{
        node:null,
        name:"starNode/totalCoin",
    },
    coinSpeed:{
        node:null,
        name:"starNode/coinInrSpeed"
    },
    btn_coin_0:{
        node:null,
        name:'btn_coin_0',
        eventType:"click",
        cb:"onBtnCoinClick",
        active:false,
    },
    btn_coin_1:{
        node:null,
        name:'btn_coin_1',
        eventType:"click",
        cb:"onBtnCoinClick",
        active:false,
    },
    btn_coin_2:{
        node:null,
        name:'btn_coin_2',
        eventType:"click",
        cb:"onBtnCoinClick",
        active:false,
    },
    
}
cc.Class({
    extends: base,

    properties: {
        
    },


    init(){
        this._super(viewConfig);
        cc.tg.eventCenter.addEvent("updateCoin",(data)=>{
            //更新金币
            this.updateInfo(viewConfig["coinLabel"].node,data,cc.Label);
        })
        cc.tg.eventCenter.addEvent("updateTxCoin",(data)=>{
            // 更新提现金币
            this.updateInfo(viewConfig["txCoin"].node,data,cc.Label);
        })
        cc.tg.eventCenter.addEvent("totalCoin",(data)=>{
            // 更新总金币
            this.updateInfo(viewConfig["totalCoin"].node,data,cc.Label);
        });

        cc.tg.eventCenter.addEvent("coinSpeed",(data)=>{
            this.updateInfo(viewConfig["coinSpeed"].node,data,cc.Label);
        });

        cc.tg.eventCenter.addEvent("showCoin",(data)=>{
            // 显示动态金币
           // this.showCoin(viewConfig["btn_coin_0"].node,"125");
           for(let i = 0 ; i < data.length;++i)
           {
               if(i == 0)
               {
                    this.showCoin(viewConfig["btn_coin_0"].node,data[i].currentGetGoldNumber,i);
               }
               else if(i == 1)
               {
                    this.showCoin(viewConfig["btn_coin_1"].node,data[i].currentGetGoldNumber,i);
               }
               else if( i == 2)
               {
                    this.showCoin(viewConfig["btn_coin_2"].node,data[i].currentGetGoldNumber,i);
               }
           }
        })

        this.isInited = true;
    },


    //击开
    onBtnOpenClick(){
        cc.log("open");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"openHB",active:true})
    },
    // 提现
    onBtnTxClick(){
        cc.log("tx");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"tx",active:true});
    },
    //排行榜
    onBtnRankClick(){
        cc.log("rank");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"rank",active:true});
    },
    // 点击玩法
    onBtnRuleClick(){
        cc.log("rule");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"rule",active:true});
    },
    // 自己的信息
    onBtnMyinfoClick(){
        cc.log("my info");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"myInfo",active:true});
    },
    // 音效
    onBtnAudioClick(){
        cc.log("audio");
        cc.tg.audioMgr.setVolume(0.0);
    },
    // 签到
    onBtnSignClick(){
        cc.log("sign");
       cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true});
       let cb = ()=>{
            cc.tg.gameLogic.toSign();
            cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"sign",active:true});
       }
       cc.tg.eventCenter.dispatchEvent("toShowAd",{codeId:"sign_Tip_ex",cb:cb,countDown:3})
    },

    // 任务
    onBtnTaskClick(){
        cc.log("task");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"task",active:true});
    },
    // 大转盘
    onBtnTurntableClick(){
        cc.log("tt");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"turntable",active:true});
    },
    // 刮刮卡
    onBtnGGKClick(){
        cc.log("ggk");
        cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"ggk",active:true});
    },
    onBtnhbClick(){
       // cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true});
       cc.tg.eventCenter.dispatchEvent("onHBRain");
    },
    onBtnCoinClick(e){
      //  cc.tg.adDataCenter.toShowAd('sys_re_coin_ex');
       let slot = e.target.slot;
       cc.tg.gameLogic.toGetCoin(slot);
       cc.tg.eventCenter.dispatchEvent("toShowAd",{codeId:"sys_re_coin_re",countDown:3});
       cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true});
    },

    showCoin(node,coinV,slot){
        if(!node)
            return ;
        let label = cc.find("Background/Label",node);
        if(label)
        {
            this.updateInfo(label,coinV,cc.Label);
        }
        node.slot = slot;
        node.stopAllActions();
        node.active = true;
        node.on("click",this.onBtnCoinClick,this);
        let moveUp = cc.moveBy(2,cc.v2(0,10))
        let moveDown = cc.moveBy(2,cc.v2(0,-10))
        node.runAction(cc.repeatForever(cc.sequence(moveUp,moveDown)));
    },
    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },
});
